package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;

import cn.itcast.pojo.Address;
import cn.itcast.service.BranchCourtsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/aress")
//开启跨域访问
@CrossOrigin
public class BranchCourtsController {
    @Reference
    private BranchCourtsService branchCourtsService;

/**
 * 查询所有分院并分页展示
 */

//    @GetMapping(path = "/findAll")
//    public Result findPage(QueryPageBean queryPageBean) {
//
//        try {
//            PageResult pageResult = branchCourtsService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
//            return new Result(true, MessageConstant.QUERY_BranchCourts_SUCCESS, pageResult);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.QUERY_BranchCourts_FAIL);
//        }
//    }
@GetMapping(path = "/findAll")
public Result findAll() {
    //不分页查询分院信息
    try {
        List<Address> addressList = branchCourtsService.findAll();
        return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, addressList);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
    }
}

    /**
     * 新建分院地址
     * @param address
     * @return
     */
    @PutMapping(path = "/submit")
    public Result submit(@RequestBody Address address){
    try {
        branchCourtsService.submit(address);
        return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(true, MessageConstant.ADD_ADDRESS_FAIL);
    }
}
/**
 * 更新(修改)分院信息
 */
    @PutMapping(path = "/update")
    public Result update(@RequestBody Address address ,String checkitemIds){
        try {
            branchCourtsService.update(address, checkitemIds);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }
    /**
     * 删除分院信息
     */
    @DeleteMapping(path = "/{id}")
    public Result delete(@PathVariable("id") Integer id){
        try {
            branchCourtsService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

}

